﻿<?php 
require_once '../../includefiles.php';
include_once("../../model/users.php");
require_once '../../model/admindetails.php';
include_once '../../model/email.php';
include_once '../../model/lead_messages.php';


$userObj = new Model_Users();
$adminObj = new Model_AdminDetails();
$emailObj = new Model_Email();
$leadMessage = new Model_LEAD_MESSAGE();
## Check user login or not?
$login_id = $_SESSION['getbids_id'];		

if($login_id == ""){
?> 
	<script type="text/javascript">		
	//SITEROOT = "http://192.168.1.42/getbids/";						
	SITEROOT = "<?php echo SITE_URL ?>/";
		
	var redirectPage = SITEROOT + "signin/";	
	window.parent.location.href = redirectPage;		
	
	</script>
<?php
	exit;		
} 
if($_POST['jobid'] && $_POST['trade_id'] && $_POST['jobowner_id'] && $_POST['message'])
{
	$jobid = $_POST['jobid'];
	$trade_id = $_POST['trade_id'];
	$jobownerid = $_POST['jobowner_id'];
	$message =  $_POST['message'];
	$file = $_FILES['attachment'];
	if(!is_file(BASEFOLDER.'uploads/messages/lead'.$jobid))
		@mkdir(BASEFOLDER.'uploads/messages/lead'.$jobid, 0777);
	if($file['name'] && $file['size'] < 5000000)
	{
		move_uploaded_file($file['tmp_name'],	BASEFOLDER.'uploads/messages/lead'.$jobid.'/'.$file['name']);
	}
	$attachment = $file['name'];
	$array= array('lead_id' => $jobid,'trade_id'=>$trade_id,'job_owner_id'=>$jobownerid,'message'=>$message,'attachfile'=>$attachment,'created'=>date('Y-m-d h:i:s'),'type'=>'client');
	$leadMessage->addMessages($array);
}

$jobID =base64_decode($_GET['id1']);
$getjobdetil = $userObj->getJobDetailsById($jobID);
$seekid = $_GET['id2'];
$seeker = $userObj->getUserDetailsByUserId($seekid);

$jobowner = $seeker['first_name'] . ' '. $seeker['last_name'];
$messageslist  = $leadMessage->getMessages($jobID,$seeker['id'],$login_id);

for($i=0;$i<count($messageslist);$i++)
{
	$messageslist[$i]["message"] = str_replace(chr(10),"<br>",$messageslist[$i]["message"]);	
	$messageslist[$i]["created"] = date('D d F Y',strtotime($messageslist[$i]["created"]));	
}


$smarty->assign('messageslist',$messageslist);
$smarty->assign('jobowner',$jobowner);
$smarty->assign('jobowner_id',$login_id);
$smarty->assign('jobid',$jobID);
$smarty->assign('trade_id',$seeker['id']);
$smarty -> assign("getjobdetil",$getjobdetil);
$smarty->display(TEMPLATEDIR . 'modules/jobs/message.tpl');



?>